﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.AdminCustomer
{
    public partial class Permissions : BaseForm
    {
        Accounts_PermissionsSvc.SoapHeaderClass sopaHeader = new Accounts_PermissionsSvc.SoapHeaderClass();
        public Permissions()
        {
            InitializeComponent();
        }

        private void Permissions_Load(object sender, EventArgs e)
        {
            BindComboBox();
        }

        private void BindComboBox()
        {
            DataSet ds = null;
            Accounts_RolesSvc.SoapHeaderClass sopaHeaderRole = new Accounts_RolesSvc.SoapHeaderClass();
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            Cursor.Current = Cursors.WaitCursor;
            ServiceManipulator.SafeAsyncExecution<Accounts_RolesSvc.Accounts_RolesServiceSoapClient>(
                proxy => proxy.GetList(sopaHeaderRole, ""),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        cbbRoles.DisplayMember = "Description";
                        cbbRoles.ValueMember = "RoleID";
                        cbbRoles.DataSource = ds.Tables[0];
                        cbbRoles.SelectedIndex = 0;

                        SetCheck(ckbList);
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }
        //提交
        private void btnAdd_Click(object sender, EventArgs e)
        {
            GetCheckedItems(ckbList);
        }
        //初始化
        public void SetCheck(CheckedListBox cblItems)
        {
            DataSet ds = null;
            Accounts_PermissionsSvc.SoapHeaderClass sopaHeaderper = new Aliyeye.Car.Net.UI.Accounts_PermissionsSvc.SoapHeaderClass();
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            Cursor.Current = Cursors.WaitCursor;
            string strWhere = " CategoryID=" + cbbRoles.SelectedValue;
            ServiceManipulator.SafeAsyncExecution<Accounts_PermissionsSvc.Accounts_PermissionsServiceSoapClient>(
                 proxy => proxy.GetList(sopaHeaderper, strWhere),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                            {
                                for (int i = 0; i < cblItems.Items.Count; i++)
                                {
                                    if (cblItems.GetItemText(cblItems.Items[i]) == ds.Tables[0].Rows[j]["Description"].ToString())
                                    {
                                        cblItems.SetItemChecked(i, true);
                                    }
                                }
                            }
                        }

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }

        //更新
        public void GetCheckedItems(CheckedListBox cblItems)
        {
            Accounts_PermissionsSvc.Accounts_Permissions model = new Accounts_PermissionsSvc.Accounts_Permissions();
            Accounts_PermissionsSvc.SoapHeaderClass sopaHeaderper = new Aliyeye.Car.Net.UI.Accounts_PermissionsSvc.SoapHeaderClass();
            int CategoryID = Convert.ToInt32(cbbRoles.SelectedValue);

            ServiceManipulator.SafeAsyncExecution<Accounts_PermissionsSvc.Accounts_PermissionsServiceSoapClient>(
                    proxy => proxy.DeleteCoun(sopaHeaderper, CategoryID),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            bool boolResult = true;
                            for (int i = 0; i < cblItems.Items.Count; i++)
                            {
                                if (cblItems.GetItemChecked(i))
                                {
                                    Accounts_PermissionsSvc.Accounts_Permissions modelTemp = new Aliyeye.Car.Net.UI.Accounts_PermissionsSvc.Accounts_Permissions();
                                    Accounts_PermissionsSvc.Accounts_PermissionsServiceSoapClient client = new Aliyeye.Car.Net.UI.Accounts_PermissionsSvc.Accounts_PermissionsServiceSoapClient();
                                    modelTemp.CategoryID = CategoryID;
                                    modelTemp.Description = string.Format("{0}", cblItems.GetItemText(cblItems.Items[i]));

                                    int intResult = client.Add(sopaHeaderper, modelTemp);
                                    if (intResult <= 0)
                                        boolResult = false;
                                }
                            }

                            if (boolResult)
                                PageCommon.Notify("操作成功！");
                            else
                                PageCommon.Alert("操作失败，请重新操作！");

                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );

            //model.CategoryID = CategoryID;
            //for (int i = 0; i < cblItems.CheckedItems.Count; i++)
            //{
            //    if (cblItems.GetItemChecked(i))
            //    {
            //        Accounts_PermissionsSvc.Accounts_Permissions modelTemp = new Aliyeye.Car.Net.UI.Accounts_PermissionsSvc.Accounts_Permissions();
            //        modelTemp.CategoryID = CategoryID;
            //        modelTemp.Description = string.Format("{0}", cblItems.GetItemText(cblItems.Items[i]));
            //        ServiceManipulator.SafeAsyncExecution<Accounts_PermissionsSvc.Accounts_PermissionsServiceSoapClient>(
            //       proxy => proxy.Add(sopaHeaderper, model),
            //       result =>
            //       {
            //           if (result.Error == null)
            //           {
            //               overlay.FadeOut();Cursor.Current = Cursors.Default;
            //           }
            //           else
            //           {
            //               PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
            //           }
            //       },
            //       this
            //   );
            //    }
            //}

        }


        //查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            ckbAll.Checked = false;
            SelectNotAll();
            SetCheck(ckbList);
        }

        private void cbbRoles_TextChanged(object sender, EventArgs e)
        {
            ckbAll.Checked = false;
            SelectNotAll();
            SetCheck(ckbList);
        }

        private void ckbAll_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbAll.Checked)
                SelectAll();
            else
                SelectNotAll();
        }

        private void SelectAll()
        {
            for (int i = 0; i < ckbList.Items.Count; i++)
            {
                ckbList.SetItemChecked(i, true);
            }
        }

        private void SelectNotAll()
        {
            for (int i = 0; i < ckbList.Items.Count; i++)
            {
                ckbList.SetItemChecked(i, false);
            }
        }

    }
}
